//
//  RBSessionManager.m
//  NiXunApp
//
//  Created by Riber on 2017/10/12.
//  Copyright © 2017年 dookay_73. All rights reserved.
//

#import "RBSessionManager.h"

static NSTimeInterval const RBSessionConnectTimeInterval = 2.0;

@interface RBSessionManager ()

@property (nonatomic, strong)  AFHTTPRequestSerializer <AFURLRequestSerialization> *normalReqSerializer;
@property (nonatomic, strong)  AFJSONRequestSerializer <AFURLRequestSerialization> *jsonReqSerializer;

@property (nonatomic, strong)  AFHTTPResponseSerializer <AFURLResponseSerialization> *normalRespSerializer;
@property (nonatomic, strong)  AFJSONResponseSerializer <AFURLResponseSerialization> *jsonRespSerializer;

@end

@implementation RBSessionManager

+(RBSessionManager *)shareManager {
    static RBSessionManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [RBSessionManager manager];
    });
    
    return manager;
}


- (AFHTTPRequestSerializer *)normalReqSerializer {
    if (!_normalReqSerializer) {
        _normalReqSerializer = [AFHTTPRequestSerializer serializer];
        [_jsonReqSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        _normalReqSerializer.timeoutInterval = RBSessionConnectTimeInterval;
    }
    return _normalReqSerializer;
}

- (AFJSONRequestSerializer *)jsonReqSerializer {
    if (!_jsonReqSerializer) {
        _jsonReqSerializer = [AFJSONRequestSerializer serializer];
        [_jsonReqSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        _jsonReqSerializer.timeoutInterval = RBSessionConnectTimeInterval;
    }
    return _jsonReqSerializer;
}

- (AFHTTPResponseSerializer *)normalRespSerializer {
    if (!_normalRespSerializer) {
        _normalRespSerializer = [AFHTTPResponseSerializer serializer];
        _normalRespSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    }
    return _normalRespSerializer;
}

- (AFJSONResponseSerializer *)jsonRespSerializer {
    if (!_jsonRespSerializer) {
        _jsonRespSerializer = [AFJSONResponseSerializer serializer];
        _jsonRespSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
    }
    return _jsonRespSerializer;
}

- (void)setupNormalConfigureStyle:(RBNetworkingStyle)netStyle {
    switch (netStyle) {
        case RBNetworkingStyleNormal:
            self.requestSerializer = self.normalReqSerializer;
            self.responseSerializer = self.normalRespSerializer;
            break;
        case RBNetworkingStyleJsonReq:
            self.requestSerializer = self.jsonReqSerializer;
            self.responseSerializer = self.normalRespSerializer;
            break;
        case RBNetworkingStyleJsonResp:
            self.requestSerializer = self.normalReqSerializer;
            self.responseSerializer = self.jsonRespSerializer;
            break;
        case RBNetworkingStyleJsonReqAndJsonResp:
            self.requestSerializer = self.jsonReqSerializer;
            self.responseSerializer = self.jsonRespSerializer;
            break;
        default:
            self.requestSerializer = self.jsonReqSerializer;
            self.responseSerializer = self.jsonRespSerializer;
            break;
    }
}

@end
